const multer = require('@koa/multer');
const getDateFileName = function() {
    let res = ``;
    const date = new Date(),
        y = date.getFullYear(),
        tm = date.getMonth()+1,
        m = tm < 10 ? '0'+ tm : tm,
        td = date.getDate(),
        d = td < 10 ? '0'+ td : td;
    res = `${y}${m}${d}`;
    return res;
}
const curTime = getDateFileName();
const upload_avatars = multer({dest: 'app/public/avatars/'});  // upload avatars
const upload_image = multer({dest: `app/public/image/${curTime}/`});  // upload image
const upload_video = multer({dest: `app/public/video/${curTime}/`});  // upload video


module.exports = app => {
    const { router, controller } = app;
    const permission = app.jwt;
    /** user ability */
    router.get('/api/user/list', permission, controller.user.user_list);
    router.post('/api/user/login', controller.user.user_login);
    router.get('/api/user/info', permission, controller.user.user_info);
    router.post('/api/user/register', controller.user.user_register);
    router.post('/api/user/add', permission, controller.user.user_add);
    router.post('/api/user/update', permission, controller.user.user_update);
    router.get('/api/user/logout', permission, controller.user.user_logout);
    router.delete('/api/user/delete', permission, controller.user.user_delete);
    /** member ability */
    router.get('/api/member/list', permission, controller.member.user_list);
    router.post('/api/member/login', controller.member.user_login);
    router.get('/api/member/info', permission, controller.member.user_info);
    router.post('/api/member/register', controller.member.user_register);
    // router.post('/api/member/add', permission, controller.member.user_add);
    router.post('/api/member/update', permission, controller.member.user_update);
    router.get('/api/member/logout', permission, controller.member.user_logout);
    router.delete('/api/member/delete', permission, controller.member.user_delete);
    /** role */
    router.post('/api/role/add', permission, controller.role.role_add);
    router.get('/api/role/list', permission, controller.role.role_list);
    router.post('/api/role/update', permission, controller.role.role_update);
    router.post('/api/role/del', permission, controller.role.role_del);
    /** grade */
    router.post('/api/grade/add', permission, controller.grade.grade_add);
    router.get('/api/grade/list', permission, controller.grade.grade_list);
    router.post('/api/grade/update', permission, controller.grade.grade_update);
    router.post('/api/grade/del', permission, controller.grade.grade_del);
    /** dict */
    router.post('/api/dict/add', permission, controller.dict.dict_add);
    router.get('/api/dict/list', permission, controller.dict.dict_list);
    router.post('/api/dict/update', permission, controller.dict.dict_update);
    router.post('/api/dict/del', permission, controller.dict.dict_del);
    /** user identify */
    router.post('/api/sendEmailCode', controller.user.send_emailCode);
    router.post('/api/identifyEmailCode', controller.user.identify_emailCode);
    /** update avatars */
    router.post('/api/updateAvatars', upload_avatars.single('avatars'), controller.user.user_avatars);
    /** article ability */
    router.post('/api/label/edit', permission, controller.label.label_edit);
    router.get('/api/label/list', permission, controller.label.label_list);
    router.delete('/api/label/del', permission, controller.label.label_del);
    router.post('/api/article/edit', permission, controller.article.article_edit);
    router.get('/api/article/list', controller.article.article_list);
    router.get('/api/article/detail', controller.article.article_detail);
    router.delete('/api/article/del', permission, controller.article.article_del);
    router.post('/api/article/collect', permission, controller.article.article_collect);  // 文章收藏
    /** comment ability */
    router.post('/api/comment/add', controller.comment.comment_add);  // 添加评论
    router.post('/api/comment/list', controller.comment.comment_list);  // 评论列表
    router.post('/api/reply/list', controller.comment.reply_list);  // 回复列表
    /** upload image */
    router.post('/api/upload/image', permission, upload_image.single('image'), controller.upload.upload_files);
    router.post('/api/upload/video', permission, upload_video.single('video'), controller.upload.upload_files);
    /** download file */
    router.post('/api/download/file', controller.upload.download_file);
    /** menu */
    router.get('/api/menu/list', permission, controller.menu.menu_list);
    router.post('/api/menu/add', permission, controller.menu.menu_add);
    router.post('/api/menu/update', permission, controller.menu.menu_update);
    router.delete('/api/menu/del', permission, controller.menu.menu_del);
}